<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<!-- react 核心库 -->
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<!-- dom相关库 -->
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<!-- Es6->Es5 jsx->js -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
 
<div id="app"></div>
<!-- 使用JSX，属性使用text/babel -->
<script type="text/babel">

    //1.创建组件
    class Life extends React.Component{
        state = {
            opacity: 0.5
        }
        death = ()=>{
            //卸载组件
            ReactDOM.unmountComponentAtNode(document.getElementById("app"))
        }

        //组件挂载完毕
        componentDidMount(){
            this.timer = setInterval(()=>{
                let {opacity} = this.state
                opacity -= 0.1
                if(opacity <= 0){
                    opacity = 1
                }
                this.setState({opacity})
            }, 200)
        }

        componentWillUnmount(){
            //清除定时器
            clearInterval(this.timer)
        }

        render(){
            return (
                <div>
                    <h2 style = {{opacity: this.state.opacity}}>React学不会怎么办</h2>    
                    <button onClick={this.death}>不活了</button>
                </div>
            )
        }
    }

    

    //渲染组件
    ReactDOM.render(<Life/>, document.getElementById("app"))
</script>
 
</body>
</html>